<template>
  <div class="mod-activiti__running">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.instanceId" placeholder="流程实例ID" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.definitionKey" placeholder="流程定义Key" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="processInstanceId"
        header-align="center"
        align="center"
        label="流程实例ID">
      </el-table-column>
      <el-table-column
        prop="processDefinitionKey"
        header-align="center"
        align="center"
        label="流程定义Key">
      </el-table-column>
      <el-table-column
        prop="processDefinitionId"
        header-align="center"
        align="center"
        label="流程定义ID">
      </el-table-column>
      <el-table-column
        prop="processDefinitionName"
        header-align="center"
        align="center"
        label="流程定义名称">
      </el-table-column>
      <el-table-column
        prop="activityId"
        header-align="center"
        align="center"
        label="当前环节">
      </el-table-column>
      <el-table-column
        prop="suspended"
        header-align="center"
        align="center"
        label="是否挂起">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.suspended" size="small">是</el-tag>
          <el-tag v-else size="small" type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="deleteHandle(scope.row.processInstanceId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          instanceId: '',
          definitionKey: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http.get(
          `${window.SITE_CONFIG['baseURL']}/act/process/running`,
          {
            params: {
              'page': this.pageIndex,
              'limit': this.pageSize,
              'instanceId': this.dataForm.instanceId,
              'definitionKey': this.dataForm.definitionKey
            }
          }
        ).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data
            this.totalPage = data.count
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定对[流程实例id=${id}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(
            `${window.SITE_CONFIG['baseURL']}/act/process/deleteInstance`,
            {
              'instanceId': id
            },
            {
              headers: { 'content-type': 'application/x-www-form-urlencoded' }
            }
          ).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
